<!--
* @FileDescription: 编辑角色数据权限
* @Author: zhangjing
* @Date: 2021-9-28 3:27:32 ?F10: PM?
* @LastEditors: zhangjing
* @LastEditTime: 2021-9-28 3:27:32 ?F10: PM?
 -->
<template>
	<globalModal :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel">
		<div slot="title">
			编辑角色
			<span style="color: #f00;">({{ (mdl && mdl.roleName) || '' }})</span>
			的数据权限
		</div>
		<a-spin :spinning="confirmLoading" style="min-height: 100px;">
			<a-form :form="form" autocomplete="off" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<a-form-item help="特殊情况下，设置为“自定数据权限”" label="数据权限">
					<a-select allowClear @change="changeDataScope" v-decorator="['dataScope', { rules: [{ required: true, message: '数据权限' }] }]" placeholder="请选择数据权限">
						<a-select-option :value="item.key" v-for="(item, index) in datascopelist" :key="index">{{ item.value }}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item v-if="dataScope == '2'" label="数据权限">
					<div style="max-height:270px;overflow: auto;">
						<a-tree
							checkable
							@check="onCheck"
							:checkedKeys="checkedKeys"
							:treeData="treeData"
							@expand="onExpand"
							:expandedKeys="expandedKeys"
							:checkStrictly="checkStrictly"
							:replaceFields="replaceFields"
						></a-tree>
					</div>
				</a-form-item>
			</a-form>
		</a-spin>
	</globalModal>
</template>

<script>
import { sysManageapi } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
import { DATASCOPE } from '@/utils/enumeration.js';
export default {
	name: 'editRoleDataModal',
	data() {
		return {
			roleId: '',
			dataScope: '',
			permissions: [],
			orgList: [],
			datascopelist: [],
			treeData: [],
			checkedKeys: [],
			expandedKeys: [],
			allTreeKeys: [],
			autoExpandParent: true,
			checkStrictly: true, //默认父子关联
			replaceFields: { children: 'children', title: 'deptName', key: 'deptId', value: 'deptId' },
			orgId: ''
		};
	},
	mixins: [ModalMixins],
	created() {
		let list = DATASCOPE.map((value, index) => {
			return {
				key: (index + 1).toString(),
				value
			};
		});
		this.datascopelist = list;
	},
	methods: {
		show(record) {
			console.log("机构id"+JSON.stringify(this.mdl.orgId))
			this.title = '编辑角色数据权限';
			this.roleId = record.roleId;
			this.mdl = Object.assign({}, { dataScope: 1, deptIds: [] }, record);
			this.visible = true;
			this.dataScope = this.mdl.dataScope;
			this.expandedKeys = [];
			this.checkedKeys = [];
			if (this.orgId != this.mdl.orgId) {
				this.orgId = this.mdl.orgId
				this.treeData = [];
			}
			if (this.dataScope == 2) {
				let promiseList = [];
				promiseList.push(this.getRoleJoinDatascope());
				if (this.treeData.length == 0) {
					promiseList.push(this.getDeptTree());
				} else {
					/* 默认设置部门树展开第一层 */
					this.getExpandedKeys(this.treeData);
				}

				this.confirmLoading = true;
				Promise.all(promiseList).finally(() => {
					this.confirmLoading = false;
				});
			}
			this.$nextTick(() => {
				this.form.setFieldsValue(this.$pick(this.mdl, 'dataScope'));
			});
		},
		//查询角色关联的部门
		getRoleJoinDatascope() {
			this.$postAction(sysManageapi.roleUrl.roleByDept + '?roleId=' + this.roleId).then(res => {
				if (res && res.code == 1) {
					this.checkedKeys = res.data;
				}
			});
		},
		//查询所有部门
		getDeptTree() {
			this.$postAction(sysManageapi.deptUrl.getList, {
				orgId: this.mdl.orgId
			}).then(res => {
				if (res && res.code == 1) {
					this.treeData = [...res.data];
					this.getExpandedKeys(this.treeData);
				}
			});
		},
		//切换数据权限
		changeDataScope(e) {
			this.dataScope = e;
			if (this.dataScope == 2 && this.treeData.length == 0) {
				this.getDeptTree();
			}
		},
		handleOk() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					let parameter = { roleId: this.mdl.roleId, dataScope: this.dataScope };
					parameter.deptIds = this.dataScope == 2 ? this.checkedKeys : [];
					this.saveInfo(sysManageapi.roleUrl.setRoleDept, parameter);
				}
			});
		},
		//展开部门第一层
		getExpandedKeys(tree) {
			let list = [];
			if (tree && tree.length > 0) {
				tree.forEach(item => {
					list.push(item.deptId);
				});
			}
			this.expandedKeys = [...list];
		},

		onExpand(expandedKeys) {
			this.expandedKeys = expandedKeys;
			this.autoExpandParent = false;
		},
		onCheck(o, e) {
			if (this.checkStrictly) {
				this.checkedKeys = o.checked;
			} else {
				this.checkedKeys = o;
			}

			/* 数组去重 */
			this.checkedKeys = Array.from(new Set(this.checkedKeys));
		}
	}
};
</script>

<style scoped></style>
